package com.resou.reader.bookstore.freezone.fragment;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;
import android.util.Log;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.CommonData;
import com.resou.reader.api.entry.FreezoneBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.BookService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.bookstore.model.BookStore;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.disposables.Disposable;
import rx.schedulers.Schedulers;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-03 -21
 **/
public class FragmentFreezonPresenter extends ResouBasePresenter<IFreezonContract.IFreenzonView> implements IFreezonContract.IFreenzonPresenter {
    BookService bookStore;
    private static final String TAG = "FragmentFreezonPresente";

    public FragmentFreezonPresenter(IFreezonContract.IFreenzonView view) {
        super(view);
        bookStore = ApiImp.getInstance().getService(BookService.class);
    }

    @Override
    public void quearFreezonData(int typeId, int gender, int page) {
        mView.showProgress();
        bookStore.quearFreeZon(typeId, gender, page, pageSize)
                .subscribeOn(getIOSchedulers()).observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<FreezoneBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<FreezoneBean>> commonDataResult) {
                        if (commonDataResult.getData() != null && commonDataResult.getData().size() > 0) {
                            if (commonDataResult.getData().size() < 15) {
                                mView.showFreezonData(commonDataResult.getData());
                                mView.getmSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            } else {
                                mView.showFreezonData(commonDataResult.getData());
                                mView.getmSmartRefreshLayout().finishLoadMore();
                            }
                        } else {
                            if (Integer.valueOf(page) == 0) {
                                mView.showEmpty();
                            } else {
                                mView.getmSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showContent();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }
}
